package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.base.BaseBindingRecyclerViewAdapter;
import com.cloudcreativity.blyg.base.BaseDialogImpl;
import com.cloudcreativity.blyg.databinding.ItemLayoutOrderDetailDialogBinding;
import com.cloudcreativity.blyg.databinding.LayoutDialogOrderDetailBinding;
import com.cloudcreativity.blyg.entity.OrderDetail;
import com.cloudcreativity.blyg.entity.Orders;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class OrderDetailDialog extends Dialog {

    public ObservableField<String> orderNum = new ObservableField<>();
    public ObservableField<String> time = new ObservableField<>();
    public BaseBindingRecyclerViewAdapter<OrderDetail.Info, ItemLayoutOrderDetailDialogBinding> adapter;
    private BaseDialogImpl baseDialog;
    public OrderDetailDialog(@NonNull Context context,Orders orders,BaseDialogImpl baseDialog) {
        super(context, R.style.myProgressDialogStyle);
        this.baseDialog = baseDialog;
        setCanceledOnTouchOutside(false);
        LayoutDialogOrderDetailBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.layout_dialog_order_detail,
                null,
                false);
        setContentView(binding.getRoot());
        binding.setUtils(this);
        assert getWindow()!=null;
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        getWindow().getAttributes().width = metrics.widthPixels/3*2;
        getWindow().getAttributes().height = metrics.heightPixels/3*2;
        adapter = new BaseBindingRecyclerViewAdapter<OrderDetail.Info, ItemLayoutOrderDetailDialogBinding>(context) {
            @Override
            protected int getLayoutResId(int viewType) {
                return R.layout.item_layout_order_detail_dialog;
            }

            @Override
            protected void onBindItem(ItemLayoutOrderDetailDialogBinding binding, OrderDetail.Info item, int position) {
                binding.setItem(item);
            }
        };
        binding.rcvDetails.setLayoutManager(new LinearLayoutManager(context,
                LinearLayoutManager.VERTICAL,
                false));

        //加上分割线
        DividerItemDecoration itemDecoration = new DividerItemDecoration(context,DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(context.getResources().getDrawable(R.drawable.shape_item_divide));
        binding.rcvDetails.addItemDecoration(itemDecoration);

        initData(orders);
    }

    private void initData(Orders orders){
        this.time.set(orders.getOrderCreateTime());
        this.orderNum.set(orders.getOrderNo());
        HttpUtils.getInstance().getOrderDetail(orders.getOrderId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<OrderDetail>(baseDialog,false) {
                    @Override
                    public void onSuccess(OrderDetail t) {
                        adapter.getItems().clear();
                        adapter.getItems().addAll(t.getInfo());
                    }
                });
    }

    public void onClose(){
        dismiss();
    }
}
